import {Table} from 'semantic-ui-react'
import React from 'react';
import Cookie from "react-cookies";
import axios from "axios";

class ProgramTask extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            token: Cookie.load("Token"),
            username: Cookie.load("Username"),
            pid: this.props.pid,
            issues: []
        }
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
        if (this.state.pid !== this.props.pid) {
        	let mainThis = this
        	mainThis.setState({pid: this.props.pid})
            let infoUrl = "https://gitee.com/api/v5/repos/" + this.state.username + "/" + this.props.pid + "/issues?state=" + this.props.status + "&sort=created&direction=desc&page=1&per_page=20&access_token=" + this.state.token
            axios.get(infoUrl)
                .then(function (response) {
                    mainThis.setState({issues: response.data})
                });
        }
    }

    componentWillMount(){
    	let mainThis = this
		mainThis.setState({pid: this.props.pid})
        let infoUrl = "https://gitee.com/api/v5/repos/" + this.state.username + "/" + this.props.pid + "/issues?state=" + this.props.status + "&sort=created&direction=desc&page=1&per_page=20&access_token=" + this.state.token
        axios.get(infoUrl)
            .then(function (response) {
                mainThis.setState({issues: response.data})
            });
    }

    render() {
    	let te
    	if (Object.keys(this.state.issues).length==0) {
			te = <Table.Row><Table.Cell className="none-issues">暂无任务</Table.Cell></Table.Row>
    	} else {
    		te = this.state.issues.map(i=> 
    			<Table.Row key={i.id}>
    				<Table.Cell>
	    				<a href={i.html_url} target="_blank">
	    					{i.title}
	    				</a>
    				</Table.Cell>
    			</Table.Row>
    			)
    	}
        return(
            <Table.Body>
                {te}
            </Table.Body>
        )
    }
}

export default ProgramTask